

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 一、Calendar 日历类 java.util.Calendar 是从 JDK1.1 就推出的支持日期处理的工具类，它本身是一个抽象类，提供了许多可以进行日期操作的变量与方法。 源码中的介绍【译】：  Calendar类是一个抽象类，它提供了一些方法，用于在特定的时间瞬间和一组日历字段（例如年、月、月的第几天、小时等）之间进行转换，并用于操作日历字段（例如获取下周的日期）。时间上的">
<meta property="og:type" content="article">
<meta property="og:title" content="设计模式 笔记4 _ 简单工厂模式 在源码中的应用 _ Calendar 日历 _ 源码浅析 _ 使用总结 _ 建造者模式">
<meta property="og:url" content="http://example.com/2022/07/21/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B04%20_%20%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%20%E5%9C%A8%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8%20_%20Calendar%20%E6%97%A5%E5%8E%86%20_%20%E6%BA%90%E7%A0%81%E6%B5%85%E6%9E%90%20_%20%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93%20_%20%E5%BB%BA%E9%80%A0%E8%80%85%E6%A8%A1%E5%BC%8F/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 一、Calendar 日历类 java.util.Calendar 是从 JDK1.1 就推出的支持日期处理的工具类，它本身是一个抽象类，提供了许多可以进行日期操作的变量与方法。 源码中的介绍【译】：  Calendar类是一个抽象类，它提供了一些方法，用于在特定的时间瞬间和一组日历字段（例如年、月、月的第几天、小时等）之间进行转换，并用于操作日历字段（例如获取下周的日期）。时间上的">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-21T06:38:29.000Z">
<meta property="article:modified_time" content="2022-08-22T15:41:19.610Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="设计模式">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>设计模式 笔记4 _ 简单工厂模式 在源码中的应用 _ Calendar 日历 _ 源码浅析 _ 使用总结 _ 建造者模式 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="设计模式 笔记4 _ 简单工厂模式 在源码中的应用 _ Calendar 日历 _ 源码浅析 _ 使用总结 _ 建造者模式"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-21 14:38" pubdate>
          2022年7月21日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          15k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          127 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">设计模式 笔记4 _ 简单工厂模式 在源码中的应用 _ Calendar 日历 _ 源码浅析 _ 使用总结 _ 建造者模式</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="一、Calendar-日历类"><a href="#一、Calendar-日历类" class="headerlink" title="一、Calendar 日历类"></a>一、Calendar 日历类</h1><hr>
<p><code>java.util.Calendar</code> 是从 JDK1.1 就推出的支持日期处理的工具类，它本身是一个抽象类，提供了许多可以进行日期操作的变量与方法。</p>
<p>源码中的介绍【译】：</p>
<blockquote>
<p>Calendar类是一个抽象类，它提供了一些方法，用于在特定的时间瞬间和一组日历字段（例如年、月、月的第几天、小时等）之间进行转换，并用于操作日历字段（例如获取下周的日期）。时间上的一个瞬间可以用毫秒值表示，毫秒值是从1970年1月1日00:00:00.000 GMT（公历）开始的偏移量。</p>
<p>该类还提供了额外的字段和方法，用于在包外实现具体的日历系统。这些字段和方法被定义为受保护。</p>
<p>与其他区域设置敏感类一样，Calendar提供了一个类方法getInstance，用于获取这种类型的常用对象。Calendar的getInstance方法返回一个日历对象，其日历字段已用当前日期和时间初始化：</p>
</blockquote>
<p>Calendar中定义的表示时间的常量 private final static int（部分）：</p>
<table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">值</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">YEAR</td>
<td align="left">1</td>
<td align="left">年，1970~至今</td>
</tr>
<tr>
<td align="left">MONTH</td>
<td align="left">2</td>
<td align="left">月，1~12</td>
</tr>
<tr>
<td align="left">DAY_OF_MONTH</td>
<td align="left">5</td>
<td align="left">本月的日，从0开始，0~30</td>
</tr>
<tr>
<td align="left">DAY_OF_YEAR</td>
<td align="left">6</td>
<td align="left">本年的天，从1月1开始，1~366</td>
</tr>
<tr>
<td align="left">DAY_OF_WEEK</td>
<td align="left">7</td>
<td align="left">本星期的日，从星期天为1开始，1~7</td>
</tr>
<tr>
<td align="left">HOUR</td>
<td align="left">10</td>
<td align="left">当前小时（12小时制）0~11</td>
</tr>
<tr>
<td align="left">HOUR_OF_DAY</td>
<td align="left">11</td>
<td align="left">当前小时（24小时制）0~23</td>
</tr>
<tr>
<td align="left">MINUTE</td>
<td align="left">12</td>
<td align="left">当前的分钟 0~59</td>
</tr>
<tr>
<td align="left">SECOND</td>
<td align="left">13</td>
<td align="left">当前的秒 0~59</td>
</tr>
<tr>
<td align="left">MILLISECOND</td>
<td align="left">14</td>
<td align="left">当前的毫秒 0~999</td>
</tr>
</tbody></table>
<p>更多表示时间的常量属性可通过 JDK 源码 <code>java.util.Calendar</code> 查看</p>
<h2 id="1-1-内部属性"><a href="#1-1-内部属性" class="headerlink" title="1.1 内部属性"></a>1.1 内部属性</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> <span class="hljs-type">long</span> instant;		<span class="hljs-comment">// 当前时间戳</span><br><br><span class="hljs-keyword">private</span> <span class="hljs-type">int</span>[] fields;		<span class="hljs-comment">// Calendar.stamp[] (下半部分) 和 Calendar.fields[] (上半部分) 组合  	</span><br><span class="hljs-comment">// 从最小用户戳 MINIMUM_USER_STAMP 开始的伪时间戳。 </span><br><br><span class="hljs-keyword">private</span> <span class="hljs-type">int</span> nextStamp;      <span class="hljs-comment">// （计算用于指示已设置瞬间。）</span><br><br><span class="hljs-keyword">private</span> <span class="hljs-type">int</span> maxFieldIndex;  <span class="hljs-comment">// 保留已设置字段的最大索引（不包括week和year）。</span><br><span class="hljs-keyword">private</span> String type;		<span class="hljs-comment">// 日历类型, 支持 gregory、iso8601、buddhist、japanese 四种类型</span><br><span class="hljs-keyword">private</span> TimeZone zone;		<span class="hljs-comment">// 时区</span><br><span class="hljs-keyword">private</span> <span class="hljs-type">boolean</span> <span class="hljs-variable">lenient</span> <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;	<span class="hljs-comment">// 表示日历是否宽松 </span><br><span class="hljs-keyword">private</span> Locale locale;	    <span class="hljs-comment">// 表示日历语言, 默认为 en </span><br><span class="hljs-keyword">private</span> <span class="hljs-type">int</span> firstDayOfWeek, minimalDaysInFirstWeek;<br><span class="hljs-comment">// firstDayOfWeek 设置星期中第几天是第一天, 默认为0，即星期天</span><br><span class="hljs-comment">// minimalDaysInFirstWeek 设置一年中第一周所需的最少天数, 默认是1</span><br><span class="hljs-comment">// 1 --&gt;  第一周定义为包含一年中第一个月的第一天</span><br><span class="hljs-comment">// 7 --&gt; 如果必须是一整周</span><br><br></code></pre></td></tr></table></figure>
<h2 id="1-2-设置时间属性值"><a href="#1-2-设置时间属性值" class="headerlink" title="1.2 设置时间属性值"></a>1.2 设置时间属性值</h2><p>Calendar 类中一共提供了16个set方法，用于设置类的一些属性，这里列举几个常见的：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">set</span><span class="hljs-params">(<span class="hljs-type">int</span> field, <span class="hljs-type">int</span> value)</span>&#123;&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">set</span><span class="hljs-params">(<span class="hljs-type">int</span> year, <span class="hljs-type">int</span> month, <span class="hljs-type">int</span> date)</span>&#123;&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">set</span><span class="hljs-params">(<span class="hljs-type">int</span> year, <span class="hljs-type">int</span> month, <span class="hljs-type">int</span> date, <span class="hljs-type">int</span> hourOfDay, <span class="hljs-type">int</span> minute)</span>&#123;&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">set</span><span class="hljs-params">(<span class="hljs-type">int</span> year, <span class="hljs-type">int</span> month, <span class="hljs-type">int</span> date, <span class="hljs-type">int</span> hourOfDay, <span class="hljs-type">int</span> minute,</span><br><span class="hljs-params">                          <span class="hljs-type">int</span> second)</span>&#123;&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setTimeZone</span><span class="hljs-params">(TimeZone value)</span>&#123;&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setFirstDayOfWeek</span><span class="hljs-params">(<span class="hljs-type">int</span> value)</span>&#123;&#125;<br></code></pre></td></tr></table></figure>
<p>【例】修改当前的星期</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.Calendar;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">println</span><span class="hljs-params">(Object o)</span>&#123; System.out.println(o); &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">Calendar</span> <span class="hljs-variable">calendar</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>        println(<span class="hljs-string">&quot;当前时间:&quot;</span> + calendar.getTime());<br>        println(<span class="hljs-string">&quot;当前星期: &quot;</span> + calendar.get(Calendar.DAY_OF_WEEK));<br><br>        println(<span class="hljs-string">&quot;修改星期为星期日(1)&quot;</span>);<br>        calendar.set(Calendar.DAY_OF_WEEK, <span class="hljs-number">1</span>);<br>        println(<span class="hljs-string">&quot;修改后的时间: &quot;</span> + calendar.getTime());<br>        println(<span class="hljs-string">&quot;修改后的星期: &quot;</span> + calendar.get(Calendar.DAY_OF_WEEK));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>&#x3D;&#x3D;运行结果&#x3D;&#x3D;<br>当前时间:Wed Jul 20 09:26:48 CST 2022<br>当前星期: 4<br>修改星期为星期日(1)<br>修改后的时间: Sun Jul 17 09:26:48 CST 2022<br>修改后的星期: 1</p>
</blockquote>
<p>可以看到，如果只修改其中一个属性，那么就会修改为最接近的时间属性，而其他属性不会发生变化。</p>
<h2 id="1-3-获取时间属性"><a href="#1-3-获取时间属性" class="headerlink" title="1.3 获取时间属性"></a>1.3 获取时间属性</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">get</span><span class="hljs-params">(<span class="hljs-type">int</span> field)</span>&#123;&#125; <br></code></pre></td></tr></table></figure>
<p>【例】获取当前时间的一些属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.Calendar;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">println</span><span class="hljs-params">(Object o)</span>&#123; System.out.println(o); &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">Calendar</span> <span class="hljs-variable">calendar</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>        println(<span class="hljs-string">&quot;默认时区: &quot;</span> + calendar.getTimeZone());<br>        println(<span class="hljs-string">&quot;当前时间:&quot;</span> + calendar.getTime());<br>        println(<span class="hljs-string">&quot;日历类型: &quot;</span> + calendar.getCalendarType());<br>        println(<span class="hljs-string">&quot;当前年份: &quot;</span> + calendar.get(Calendar.YEAR));<br>        println(<span class="hljs-string">&quot;当前月份: &quot;</span> + calendar.get(Calendar.MONTH));<br>        println(<span class="hljs-string">&quot;当前日期: &quot;</span> + calendar.get(Calendar.DATE));<br>        println(<span class="hljs-string">&quot;当天是今年的第几个星期: &quot;</span> + calendar.get(Calendar.WEEK_OF_YEAR));<br>        println(<span class="hljs-string">&quot;当天是本月的第几个星期: &quot;</span> + calendar.get(Calendar.WEEK_OF_MONTH));<br>        println(<span class="hljs-string">&quot;当天是今年中的第几天: &quot;</span> + calendar.get(Calendar.DAY_OF_YEAR));<br>        println(<span class="hljs-string">&quot;当天是本月的第几天: &quot;</span> + calendar.get(Calendar.DAY_OF_MONTH));<br>        println(<span class="hljs-string">&quot;当前是当天的第几个小时: &quot;</span> + calendar.get(Calendar.HOUR_OF_DAY));<br>        println(<span class="hljs-string">&quot;当前是一小时的第几分钟: &quot;</span> + calendar.get(Calendar.MINUTE));<br>        println(<span class="hljs-string">&quot;当前是一分钟的第几秒: &quot;</span> + calendar.get(Calendar.SECOND));<br>        println(<span class="hljs-string">&quot;当前是一秒中的第几毫秒: &quot;</span> + calendar.get(Calendar.MILLISECOND));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="1-4-使用-Calander-计算时间"><a href="#1-4-使用-Calander-计算时间" class="headerlink" title="1.4 使用 Calander 计算时间"></a>1.4 使用 Calander 计算时间</h2><p><strong>【例1】</strong> 计算指定天数后的日期</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.text.SimpleDateFormat;<br><span class="hljs-keyword">import</span> java.util.Calendar;<br><span class="hljs-keyword">import</span> java.util.Date;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">println</span><span class="hljs-params">(Object o)</span>&#123; System.out.println(o); &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Date <span class="hljs-title function_">after</span><span class="hljs-params">(<span class="hljs-type">int</span> day)</span>&#123;<br>        <span class="hljs-type">Calendar</span> <span class="hljs-variable">calendar</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>        calendar.add(Calendar.DAY_OF_WEEK, day);<br>        <span class="hljs-keyword">return</span> calendar.getTime();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">SimpleDateFormat</span> <span class="hljs-variable">sdf</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleDateFormat</span>(<span class="hljs-string">&quot;yyyy 年 MM 月 dd 日 HH:mm:ss&quot;</span>);<br>        println(<span class="hljs-string">&quot;今天是: &quot;</span> + sdf.format(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>()));<br>        println(<span class="hljs-string">&quot;25天后是: &quot;</span> + sdf.format(after(<span class="hljs-number">25</span>)));<br>        println(<span class="hljs-string">&quot;100天后是: &quot;</span> + sdf.format(after(<span class="hljs-number">100</span>)));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>&#x3D;&#x3D;运行结果&#x3D;&#x3D;<br>今天是: 2022 年 07 月 20 日 09:43:15<br>25天后是: 2022 年 08 月 14 日 09:43:15<br>100天后是: 2022 年 10 月 28 日 09:43:15</p>
</blockquote>
<p><strong>【例2】</strong> 计算指定月份过某天的日期</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.text.SimpleDateFormat;<br><span class="hljs-keyword">import</span> java.util.Calendar;<br><span class="hljs-keyword">import</span> java.util.Date;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">println</span><span class="hljs-params">(Object o)</span>&#123; System.out.println(o); &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Date <span class="hljs-title function_">after</span><span class="hljs-params">(<span class="hljs-type">int</span> month, <span class="hljs-type">int</span> day)</span>&#123;<br>        <span class="hljs-type">Calendar</span> <span class="hljs-variable">calendar</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>        calendar.add(Calendar.MONTH, month);<br>        calendar.add(Calendar.DAY_OF_YEAR, day);<br>        <span class="hljs-keyword">return</span> calendar.getTime();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">SimpleDateFormat</span> <span class="hljs-variable">sdf</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleDateFormat</span>(<span class="hljs-string">&quot;yyyy 年 MM 月 dd 日 HH:mm:ss&quot;</span>);<br>        println(<span class="hljs-string">&quot;今天是: &quot;</span> + sdf.format(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>()));<br>        println(<span class="hljs-string">&quot;1月再过2天后是: &quot;</span> + sdf.format(after(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)));<br>        println(<span class="hljs-string">&quot;3月再过10天后是: &quot;</span> + sdf.format(after(<span class="hljs-number">3</span>, <span class="hljs-number">10</span>)));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>&#x3D;&#x3D;运行结果&#x3D;&#x3D;<br>今天是: 2022 年 07 月 20 日 09:45:27<br>1月再过2天后是: 2022 年 08 月 22 日 09:45:27<br>3月再过10天后是: 2022 年 10 月 30 日 09:45:27</p>
</blockquote>
<p><strong>【例3】</strong> 计算两个日期相差多少年、多少月、多少天<br>参考资料：<a target="_blank" rel="noopener" href="https://www.sojson.com/blog/260.html">https://www.sojson.com/blog/260.html</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">static</span> DayCompare <span class="hljs-title function_">dayCompare</span><span class="hljs-params">(String format, String fromDate, String toDate)</span>&#123;<br>    <span class="hljs-type">SimpleDateFormat</span> <span class="hljs-variable">sdf</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleDateFormat</span>(format);<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">from</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">to</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    <span class="hljs-keyword">try</span> &#123;<br>        from.setTime(sdf.parse(fromDate));<br>        to.setTime(sdf.parse(toDate));<br>    &#125; <span class="hljs-keyword">catch</span> (ParseException e) &#123;<br>        e.printStackTrace();<br>    &#125;<br>    <span class="hljs-keyword">return</span> dayCompare(from.getTime(), to.getTime());<br>&#125;<br><br><span class="hljs-keyword">static</span> DayCompare <span class="hljs-title function_">dayCompare</span><span class="hljs-params">(Date fromDate, Date toDate)</span>&#123;<br>    <span class="hljs-keyword">assert</span> toDate.compareTo(fromDate) &gt;= <span class="hljs-number">0</span> : <span class="hljs-string">&quot;【ERROR】&lt;fromDate&gt; must be earlier than &lt;toDate&gt;.&quot;</span>;<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">from</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">to</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    from.setTime(fromDate);<br>    to.setTime(toDate);<br><br>    <span class="hljs-comment">// 计算年月日</span><br>    <span class="hljs-type">int</span> <span class="hljs-variable">fromYear</span> <span class="hljs-operator">=</span> from.get(Calendar.YEAR);<br>    <span class="hljs-type">int</span> <span class="hljs-variable">fromMonth</span> <span class="hljs-operator">=</span> from.get(Calendar.MONTH);<br>    <span class="hljs-type">int</span> <span class="hljs-variable">fromDay</span> <span class="hljs-operator">=</span> from.get(Calendar.DAY_OF_MONTH);<br><br>    <span class="hljs-type">int</span> <span class="hljs-variable">toYear</span> <span class="hljs-operator">=</span> to.get(Calendar.YEAR);<br>    <span class="hljs-type">int</span> <span class="hljs-variable">toMonth</span> <span class="hljs-operator">=</span> to.get(Calendar.MONTH);<br>    <span class="hljs-type">int</span> <span class="hljs-variable">toDay</span> <span class="hljs-operator">=</span> to.get(Calendar.DAY_OF_MONTH);<br><br>    <span class="hljs-comment">// 计算差值</span><br>    <span class="hljs-type">int</span> <span class="hljs-variable">year</span> <span class="hljs-operator">=</span> toYear - fromYear;<br>    <span class="hljs-type">int</span> <span class="hljs-variable">month</span> <span class="hljs-operator">=</span> toMonth - fromMonth;<br>    <span class="hljs-type">int</span> <span class="hljs-variable">day</span> <span class="hljs-operator">=</span> toDay - fromDay;<br>    <span class="hljs-comment">// 处理负值</span><br>    <span class="hljs-keyword">if</span>(day &lt; <span class="hljs-number">0</span>)&#123;<br>        to.add(Calendar.MONTH, -<span class="hljs-number">1</span>);<br>        day += to.getActualMaximum(Calendar.DAY_OF_MONTH);<br>        month--;<br>    &#125;<br>    <span class="hljs-keyword">if</span>(month &lt; <span class="hljs-number">0</span>)&#123;<br>        to.add(Calendar.YEAR, -<span class="hljs-number">1</span>);<br>        month += to.getActualMaximum(Calendar.MONTH);<br>        year--;<br>    &#125;<br>    <span class="hljs-keyword">return</span> DayCompare.builder().day(day).month(month).year(year).build();<br>&#125;<br></code></pre></td></tr></table></figure>
<p>测试：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span>&#123;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">FORMAT</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;yyyy-MM-dd&quot;</span>;<br>    <span class="hljs-type">SimpleDateFormat</span> <span class="hljs-variable">sdf</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleDateFormat</span>(FORMAT);<br>    <span class="hljs-type">Date</span> <span class="hljs-variable">fromDate</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();<br>    <span class="hljs-type">String</span> <span class="hljs-variable">fromStr</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;2022-07-20&quot;</span>;<br>    String[] to = &#123;<span class="hljs-string">&quot;2022-08-20&quot;</span>, <span class="hljs-string">&quot;2022-09-10&quot;</span>, <span class="hljs-string">&quot;2023-05-30&quot;</span>, <span class="hljs-string">&quot;2024-01-01&quot;</span>&#125;;<br>    <span class="hljs-keyword">for</span> (String toStr : to) &#123;<br>        <span class="hljs-type">DayCompare</span> <span class="hljs-variable">dayCompare</span> <span class="hljs-operator">=</span> dayCompare(FORMAT, fromStr, toStr);<br>        System.out.printf(<br>            <span class="hljs-string">&quot;从[%s]到[%s], 相差: [%s] 年 [%s] 月 [%s] 日%n&quot;</span><br>            , fromStr, toStr, dayCompare.getYear(), dayCompare.getMonth(), dayCompare.getDay());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>&#x3D;&#x3D;测试结果&#x3D;&#x3D;<br>从[2022-07-20]到[2022-08-20], 相差: [0] 年 [1] 月 [0] 日<br>从[2022-07-20]到[2022-09-10], 相差: [0] 年 [1] 月 [21] 日<br>从[2022-07-20]到[2023-05-30], 相差: [0] 年 [9] 月 [10] 日<br>从[2022-07-20]到[2024-01-01], 相差: [1] 年 [4] 月 [12] 日</p>
</blockquote>
<p><strong>【例4】</strong> 计算两个日期各自相差多少年、多少月、多少天<br>参考资料：<a target="_blank" rel="noopener" href="https://www.sojson.com/blog/260.html">https://www.sojson.com/blog/260.html</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">static</span> DayCompare <span class="hljs-title function_">dayCompare</span><span class="hljs-params">(String format, String fromDate, String toDate)</span>&#123;<br>    <span class="hljs-keyword">assert</span> toDate.compareTo(fromDate) &gt;= <span class="hljs-number">0</span> : <span class="hljs-string">&quot;【ERROR】&lt;fromDate&gt; must be earlier than &lt;toDate&gt;.&quot;</span>;<br>    <span class="hljs-type">SimpleDateFormat</span> <span class="hljs-variable">sdf</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleDateFormat</span>(format);<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">from</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">to</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    <span class="hljs-keyword">try</span> &#123;<br>        from.setTime(sdf.parse(fromDate));<br>        to.setTime(sdf.parse(toDate));<br>    &#125; <span class="hljs-keyword">catch</span> (ParseException e) &#123;<br>        e.printStackTrace();<br>    &#125;<br>    <span class="hljs-keyword">return</span> dayCompare(from.getTime(), to.getTime());<br>&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> DayCompare <span class="hljs-title function_">dayCompare</span><span class="hljs-params">(Date fromDate, Date toDate)</span>&#123;<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">from</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">to</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    from.setTime(fromDate);<br>    to.setTime(toDate);<br><br>    <span class="hljs-type">int</span> <span class="hljs-variable">fromYear</span> <span class="hljs-operator">=</span> from.get(Calendar.YEAR);<br>    <span class="hljs-type">int</span> <span class="hljs-variable">fromMonth</span> <span class="hljs-operator">=</span> from.get(Calendar.MONTH);<br>    <span class="hljs-type">int</span> <span class="hljs-variable">fromDay</span> <span class="hljs-operator">=</span> from.get(Calendar.DAY_OF_MONTH);<br><br>    <span class="hljs-type">int</span> <span class="hljs-variable">toYear</span> <span class="hljs-operator">=</span> to.get(Calendar.YEAR);<br>    <span class="hljs-type">int</span> <span class="hljs-variable">toMonth</span> <span class="hljs-operator">=</span> to.get(Calendar.MONTH);<br>    <span class="hljs-type">int</span> <span class="hljs-variable">toDay</span> <span class="hljs-operator">=</span> to.get(Calendar.DAY_OF_MONTH);<br><br>    <span class="hljs-type">int</span> <span class="hljs-variable">year</span> <span class="hljs-operator">=</span> toYear - fromYear;<br>    <span class="hljs-comment">// 相差月份 = 目标的总月份 - 当前的总月份</span><br>    <span class="hljs-type">int</span> <span class="hljs-variable">month</span> <span class="hljs-operator">=</span> (toYear * <span class="hljs-number">12</span> + toMonth) - (fromYear * <span class="hljs-number">12</span> + fromMonth);<br><br>    <span class="hljs-comment">// 相差天数 = 时间戳 / 1000 / 3600 / 24</span><br>    <span class="hljs-type">int</span> <span class="hljs-variable">day</span> <span class="hljs-operator">=</span> (<span class="hljs-type">int</span>) ((to.getTimeInMillis() - from.getTimeInMillis()) / (<span class="hljs-number">1000</span> * <span class="hljs-number">3600</span> * <span class="hljs-number">24</span>));<br><br>    <span class="hljs-keyword">return</span> DayCompare.builder().day(day).month(month).year(year).build();<br>&#125;<br></code></pre></td></tr></table></figure>
<p>测试</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">FORMAT</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;yyyy-MM-dd&quot;</span>;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">fromStr</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;2022-07-20&quot;</span>;<br>    String[] to = &#123;<span class="hljs-string">&quot;2022-08-20&quot;</span>, <span class="hljs-string">&quot;2022-09-10&quot;</span>, <span class="hljs-string">&quot;2023-05-30&quot;</span>, <span class="hljs-string">&quot;2024-01-01&quot;</span>&#125;;<br>    <span class="hljs-keyword">for</span> (String toStr : to) &#123;<br>        <span class="hljs-type">DayCompare</span> <span class="hljs-variable">dayCompare</span> <span class="hljs-operator">=</span> dayCompare(FORMAT, fromStr, toStr);<br>        System.out.printf(<br>            <span class="hljs-string">&quot;从[%s]到[%s], 相差了: [%s] 年, 相差了 [%s] 月 相差了 [%s] 日%n&quot;</span><br>            , fromStr, toStr, dayCompare.getYear(), dayCompare.getMonth(), dayCompare.getDay());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>&#x3D;&#x3D;运行结果&#x3D;&#x3D;<br>从[2022-07-20]到[2022-08-20], 相差了: [0] 年, 相差了 [1] 月 相差了 [31] 日<br>从[2022-07-20]到[2022-09-10], 相差了: [0] 年, 相差了 [2] 月 相差了 [52] 日<br>从[2022-07-20]到[2023-05-30], 相差了: [1] 年, 相差了 [10] 月 相差了 [314] 日<br>从[2022-07-20]到[2024-01-01], 相差了: [2] 年, 相差了 [18] 月 相差了 [530] 日</p>
</blockquote>
<h1 id="二、Calender-类中的设计模式"><a href="#二、Calender-类中的设计模式" class="headerlink" title="二、Calender 类中的设计模式"></a>二、Calender 类中的设计模式</h1><hr>
<p>首先根据 Calender 类定义可以看出，它是一个实现了可序列化、可克隆、可比较的抽象类，这意味这它不能直接通过 new 来创建对象，一般是通过其继承子类来创建对象，它的实现类有三个，而在 java.util 包里的有两个，分别是 GregorianCalendar.java 和 JapaneseImperialCalendar，这里是两种不同日历类型的实现，前者 Gregorian 是 格力高日历，是公历的全称，后者是 JapaneseImperial 日本用的日历，我们通过 getInstance() 获取的Calender的对象默认是公历，即 Gregorian 。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Calendar</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span>, Cloneable, Comparable&lt;Calendar&gt; &#123;&#125;<br></code></pre></td></tr></table></figure>
<h2 id="2-1-简单工厂模式"><a href="#2-1-简单工厂模式" class="headerlink" title="2.1 简单工厂模式"></a>2.1 简单工厂模式</h2><p>我们创建 Calendar时，可以通过具体的实现类，除此之外，Calendar 提供了getInstace()直接获取实例。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Calendar <span class="hljs-title function_">getInstance</span><span class="hljs-params">()</span><br>&#123;<br>    <span class="hljs-keyword">return</span> createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));<br>&#125;<br></code></pre></td></tr></table></figure>
<p>如果是默认配置，那么则将调用 createCalendar() 方法，并传入默认的时区和地区</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Calendar <span class="hljs-title function_">createCalendar</span><span class="hljs-params">(TimeZone zone,</span><br><span class="hljs-params">                                       Locale aLocale)</span><br>&#123;<br>    <span class="hljs-comment">// 适配器模式 </span><br>    <span class="hljs-type">CalendarProvider</span> <span class="hljs-variable">provider</span> <span class="hljs-operator">=</span><br>        LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)<br>        .getCalendarProvider();<br>    <span class="hljs-comment">// 如果适配器提供了当前 TimeZone 和 Locale的 Calendar 实现，那么直接调用提供者的实例化方法</span><br>    <span class="hljs-keyword">if</span> (provider != <span class="hljs-literal">null</span>) &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-keyword">return</span> provider.getInstance(zone, aLocale);<br>        &#125; <span class="hljs-keyword">catch</span> (IllegalArgumentException iae) &#123;<br>           <span class="hljs-comment">// 返回默认的实例化</span><br>        &#125;<br>    &#125;<br><br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">cal</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>    <span class="hljs-comment">// 判断地区是否具有扩展名</span><br>    <span class="hljs-keyword">if</span> (aLocale.hasExtensions()) &#123;<br>        <span class="hljs-comment">// 获取地区所在的日历类型</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">caltype</span> <span class="hljs-operator">=</span> aLocale.getUnicodeLocaleType(<span class="hljs-string">&quot;ca&quot;</span>);<br>        <span class="hljs-keyword">if</span> (caltype != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-keyword">switch</span> (caltype) &#123;<br>                <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;buddhist&quot;</span>: <span class="hljs-comment">// 佛教日历</span><br>                    cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">BuddhistCalendar</span>(zone, aLocale);<br>                    <span class="hljs-keyword">break</span>;<br>                <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;japanese&quot;</span>: <span class="hljs-comment">// 日本日历</span><br>                    cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">JapaneseImperialCalendar</span>(zone, aLocale);<br>                    <span class="hljs-keyword">break</span>;<br>                <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;gregory&quot;</span>: <span class="hljs-comment">// 公历</span><br>                    cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">GregorianCalendar</span>(zone, aLocale);<br>                    <span class="hljs-keyword">break</span>;<br>            &#125;<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">if</span> (cal == <span class="hljs-literal">null</span>) &#123;<br>        <span class="hljs-comment">// 如果没有指定之前的三个日历类型，则根据地区的语言和国家来判断日历类型</span><br>        <span class="hljs-keyword">if</span> (aLocale.getLanguage() == <span class="hljs-string">&quot;th&quot;</span> &amp;&amp; aLocale.getCountry() == <span class="hljs-string">&quot;TH&quot;</span>) &#123;<br>            cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">BuddhistCalendar</span>(zone, aLocale);<br>        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (aLocale.getVariant() == <span class="hljs-string">&quot;JP&quot;</span> &amp;&amp; aLocale.getLanguage() == <span class="hljs-string">&quot;ja&quot;</span><br>                   &amp;&amp; aLocale.getCountry() == <span class="hljs-string">&quot;JP&quot;</span>) &#123;<br>            cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">JapaneseImperialCalendar</span>(zone, aLocale);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-comment">// 其他情况一律返回公历</span><br>            cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">GregorianCalendar</span>(zone, aLocale);<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> cal;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>通过解读 Calendar 类的源码，我们可以发现，它getInstance() 创建实例的方法，本质上就是调用了内部定义的 createCalendar()方法，创建实例时主要根据时区和地区来判断，共有三种日历类型，佛教日历、日本日历和公历。</p>
<p>比较有趣的是，在 createCalendar() 方法里用到了适配器模式，即 CalendarProvider，这里就不继续深入了，有个印象即可，在创建 Canlendar实例时，底层源码先从这个适配器提供者去获取实例，如果不为空就直接返回，否则就根据时区和地区来判断，返回相应的日历，默认则返回 GregorianCalendar 日历，即公历。</p>
<p>为什么说这是一种简单工厂模式呢？因为 Calendar 抽象类里的createCalendar() 创建实例时，直接根据该方法传入的参数来返回对应的 Calendar 实现类，符合工厂模式的思想，此时，我们可以把 Calendar就看成一个工厂，它能根据时区和地区返回对应的实例，这似乎有点未卜先知的意思，在抽象类里直接调用了该抽象类自身的实现类。</p>
<p><strong>【使用案例】</strong> 简单工厂模式创建 Calendar 实例对象，并将年月日改为2008年的1月1日</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">calendar</span> <span class="hljs-operator">=</span> Calendar.getInstance();<br>    calendar.set(Calendar.YEAR, <span class="hljs-number">2008</span>);<br>    calendar.set(Calendar.MONTH, <span class="hljs-number">1</span>);<br>    calendar.set(Calendar.DAY_OF_MONTH, <span class="hljs-number">1</span>);<br>    System.out.println(calendar.getTime());<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="2-2-建造者模式"><a href="#2-2-建造者模式" class="headerlink" title="2.2 建造者模式"></a>2.2 建造者模式</h2><p><code>java.util.Calendar</code>类是从 JDK1.1 开始出现的，在后来的版本中也升级过，如果不查看官方文档，我们可以看看其源码实现，比如在之前 createCalendar()方法里，调用了适配器类的一个方法，返回了 CalendarProvider 类型的对象，而这个类正是 JDK1.8 才出现的，这表示 Calendar 目前的这种简单工厂模式的创建实例方式，在 JDK1.8 时进行过升级。<br>通过源码结构图，我们可以看到 Calendar类里只有三个内部类，而且两个还是私有的，另一个唯一公开的则是 Builder<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/f4959e66c91744959abd05f53c4c700d.png#">https://img-blog.csdnimg.cn/f4959e66c91744959abd05f53c4c700d.png#</a> &#x3D;60%x)<br>部分源码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java">  <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * ...省略了很多注释内容</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@since</span> 1.8</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@see</span> Calendar#getInstance(TimeZone, Locale)</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@see</span> Calendar#fields</span><br><span class="hljs-comment">     */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Builder</span> &#123;<br>        <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">NFIELDS</span> <span class="hljs-operator">=</span> FIELD_COUNT + <span class="hljs-number">1</span>; <span class="hljs-comment">// +1 for WEEK_YEAR</span><br>        <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">WEEK_YEAR</span> <span class="hljs-operator">=</span> FIELD_COUNT;<br>        <span class="hljs-keyword">private</span> <span class="hljs-type">long</span> instant;<br>        <span class="hljs-keyword">private</span> <span class="hljs-type">int</span>[] fields;<br>        <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> nextStamp;<br>        <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> maxFieldIndex;<br>        <span class="hljs-keyword">private</span> String type;<br>        <span class="hljs-keyword">private</span> TimeZone zone;<br>        <span class="hljs-keyword">private</span> <span class="hljs-type">boolean</span> <span class="hljs-variable">lenient</span> <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;<br>        <span class="hljs-keyword">private</span> Locale locale;<br>        <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> firstDayOfWeek, minimalDaysInFirstWeek;<br> &#125;<br></code></pre></td></tr></table></figure>
<p>这个Builder 内部类 同样是在 JDK8 开始出现的，它和getInstance() 一样可以创建 Calendar 的实例对象，不过在创建时可以链式的设置一些属性。在设置属性时，其内部进行了一系列的初始化操作。要想了解它是如何初始化的，查看 build() 方法的源码即可：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> Calendar <span class="hljs-title function_">build</span><span class="hljs-params">()</span> &#123;<br>    <span class="hljs-comment">// 如果为空则初始化为默认</span><br>    <span class="hljs-keyword">if</span> (locale == <span class="hljs-literal">null</span>) &#123;<br>        locale = Locale.getDefault();<br>    &#125;<br>    <span class="hljs-keyword">if</span> (zone == <span class="hljs-literal">null</span>) &#123;<br>        zone = TimeZone.getDefault();<br>    &#125;<br>    Calendar cal;<br>    <span class="hljs-keyword">if</span> (type == <span class="hljs-literal">null</span>) &#123;<br>        type = locale.getUnicodeLocaleType(<span class="hljs-string">&quot;ca&quot;</span>);<br>    &#125;<br>    <span class="hljs-keyword">if</span> (type == <span class="hljs-literal">null</span>) &#123;<br>        <span class="hljs-keyword">if</span> (locale.getCountry() == <span class="hljs-string">&quot;TH&quot;</span><br>            &amp;&amp; locale.getLanguage() == <span class="hljs-string">&quot;th&quot;</span>) &#123;<br>            type = <span class="hljs-string">&quot;buddhist&quot;</span>;<br>        &#125; <span class="hljs-keyword">else</span> &#123;	<span class="hljs-comment">// 其他情况直接使用 gregory 公历</span><br>            type = <span class="hljs-string">&quot;gregory&quot;</span>;<br>        &#125;<br>    &#125;<br>    <span class="hljs-comment">// 判断类型</span><br>    <span class="hljs-keyword">switch</span> (type) &#123;<br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;gregory&quot;</span>:<br>            cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">GregorianCalendar</span>(zone, locale, <span class="hljs-literal">true</span>);<br>            <span class="hljs-keyword">break</span>;<br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;iso8601&quot;</span>:<br>            <span class="hljs-type">GregorianCalendar</span> <span class="hljs-variable">gcal</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">GregorianCalendar</span>(zone, locale, <span class="hljs-literal">true</span>);<br>            <span class="hljs-comment">// 使gcal成为序言公历</span><br>            gcal.setGregorianChange(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(Long.MIN_VALUE));<br>            <span class="hljs-comment">// 周定义与ISO 8601兼容</span><br>            setWeekDefinition(MONDAY, <span class="hljs-number">4</span>);<br>            cal = gcal;<br>            <span class="hljs-keyword">break</span>;<br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;buddhist&quot;</span>:<br>            cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">BuddhistCalendar</span>(zone, locale);<br>            cal.clear();<br>            <span class="hljs-keyword">break</span>;<br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;japanese&quot;</span>:<br>            cal = <span class="hljs-keyword">new</span> <span class="hljs-title class_">JapaneseImperialCalendar</span>(zone, locale, <span class="hljs-literal">true</span>);<br>            <span class="hljs-keyword">break</span>;<br>        <span class="hljs-keyword">default</span>:<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalArgumentException</span>(<span class="hljs-string">&quot;unknown calendar type: &quot;</span> + type);<br>    &#125;<br>    cal.setLenient(lenient);<br>    <span class="hljs-keyword">if</span> (firstDayOfWeek != <span class="hljs-number">0</span>) &#123;<br>        cal.setFirstDayOfWeek(firstDayOfWeek);<br>        cal.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek);<br>    &#125;<br>    <span class="hljs-comment">// 如果只是初始化，直接返回计算后的实例</span><br>    <span class="hljs-keyword">if</span> (isInstantSet()) &#123;<br>        cal.setTimeInMillis(instant);<br>        cal.complete();<br>        <span class="hljs-keyword">return</span> cal;<br>    &#125;<br><br>    <span class="hljs-keyword">if</span> (fields != <span class="hljs-literal">null</span>) &#123;<br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">weekDate</span> <span class="hljs-operator">=</span> isSet(WEEK_YEAR)<br>            &amp;&amp; fields[WEEK_YEAR] &gt; fields[YEAR];<br>        <span class="hljs-keyword">if</span> (weekDate &amp;&amp; !cal.isWeekDateSupported()) &#123;<br>            <span class="hljs-comment">// 不支持当前设置的星期日期, 直接抛异常</span><br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalArgumentException</span>(<span class="hljs-string">&quot;week date is unsupported by &quot;</span> + type);<br>        &#125;<br>        <span class="hljs-comment">//将字段从min stamp设置为 max stamp，以便字段解析在日历中起作用。</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">stamp</span> <span class="hljs-operator">=</span> MINIMUM_USER_STAMP; stamp &lt; nextStamp; stamp++) &#123;<br>            <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">index</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; index &lt;= maxFieldIndex; index++) &#123;<br>               ...<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-keyword">if</span> (weekDate) &#123;<br>           ...<br>        &#125;<br>        cal.complete();<br>    &#125;<br>    <span class="hljs-keyword">return</span> cal;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>代码看起来比较长，但是逻辑并不难理解，相比 createInstance()，这种方式 Builder 的build()方式似乎更复杂， 主要是因为需要处理一些默认的没有设置的属性。</p>
<p><strong>【应用案例】</strong> 使用建造者模式创建 Calendar 实例对象，并将年月日改为2008年1月1日</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>    <span class="hljs-type">Calendar</span> <span class="hljs-variable">calendar</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Calendar</span>.Builder()<br>        .set(Calendar.YEAR, <span class="hljs-number">2008</span>)<br>        .set(Calendar.MONTH, <span class="hljs-number">1</span>)<br>        .set(Calendar.DAY_OF_MONTH, <span class="hljs-number">1</span>)<br>        .build();<br>    System.out.println(calendar.getTime());<br>&#125;<br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" class="category-chain-item">设计模式</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">#设计模式</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>设计模式 笔记4 _ 简单工厂模式 在源码中的应用 _ Calendar 日历 _ 源码浅析 _ 使用总结 _ 建造者模式</div>
      <div>http://example.com/2022/07/21/设计模式 笔记4 _ 简单工厂模式 在源码中的应用 _ Calendar 日历 _ 源码浅析 _ 使用总结 _ 建造者模式/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月21日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/24/Web%E5%BC%80%E5%8F%91%20%E8%BA%AB%E4%BB%BD%E9%AA%8C%E8%AF%81%20_%20SpringBoot%20%E6%95%B4%E5%90%88%20JWT%20_%20%E5%89%8D%E5%90%8E%E7%AB%AF%E4%B8%8D%E5%88%86%E7%A6%BB%E7%9A%84%E6%A1%88%E4%BE%8B%20_%20Cookie%E3%80%81Session%E3%80%81Token%20%E3%80%81JWT%E6%A6%82%E8%BF%B0%E4%B8%8E%E6%AF%94%E8%BE%83/" title="Web开发 身份验证 _ SpringBoot 整合 JWT _ 前后端不分离的案例 _ Cookie、Session、Token 、JWT概述与比较">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Web开发 身份验证 _ SpringBoot 整合 JWT _ 前后端不分离的案例 _ Cookie、Session、Token 、JWT概述与比较</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/21/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B05%20_%20%E5%B7%A5%E5%8E%82%E6%96%B9%E6%B3%95%E6%A8%A1%E5%BC%8F%20%E5%9C%A8%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8%20_%20logback%20%E6%97%A5%E5%BF%97%E7%AE%A1%E7%90%86%E6%A1%86%E6%9E%B6%E5%90%AB%E4%B9%89%E3%80%81%E6%9E%B6%E6%9E%84%E4%BB%A5%E5%8F%8A%E9%85%8D%E7%BD%AE%20_%20logback%20%E4%BD%BF%E7%94%A8%E6%A1%88%E4%BE%8B%20_%20logback%E4%B8%AD%E5%AD%98%E5%9C%A8%E7%9A%84%E5%B7%A5%E5%8E%82%E6%96%B9%E6%B3%95%E6%A8%A1%E5%BC%8F%E5%92%8C%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F%20_/" title="设计模式 笔记5 _ 工厂方法模式 在源码中的应用 _ logback 日志管理框架含义、架构以及配置 _ logback 使用案例 _ logback中存在的工厂方法模式和单例模式 _">
                        <span class="hidden-mobile">设计模式 笔记5 _ 工厂方法模式 在源码中的应用 _ logback 日志管理框架含义、架构以及配置 _ logback 使用案例 _ logback中存在的工厂方法模式和单例模式 _</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
